package fy.hr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fy.hr.dao.RequestNoTimerTask;
import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.config.FyConfig;
import fy.hr.bean.WorkOverTimeFormBean;
import fy.util.DBUtil;

public class WorkOverTimeFormDao {
	private static Log log = LogFactory.getLog(WorkOverTimeFormDao.class);
	private List<String> pk_psndocList;
	private WorkOverTimeFormBean workOverTimeFormBean;
	private final static String FIND_PERSONINFO_STR = "select a.pk_psndoc,b.PK_TBM_PSNDOC,a.pk_deptdoc,a.pk_om_job,a.pk_corp from bd_psndoc a join tbm_psndoc b on  a.PK_PSNDOC=b.pk_psndoc where  a.PSNCLSCOPE=0 and psncode=?";
	private final static String FIND_OVERTIMETYPE_STR = "select pk_timeitem  from tbm_timeitem where fathertimeitem='overtimeclass' and pk_corp=?";
	boolean success = false;

	public boolean InsertHR() throws Exception {
		// System.out.println(this.workOverTimeFormBean.getOvertimetype());
		for (int i = 0; this.workOverTimeFormBean.getPersonCodeList().size() > i; i++) {
			//System.out.println(
			//		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + this.workOverTimeFormBean.getPersonCodeList().size());
			//System.out.println("~~~~~~~~~~~~~~~~~~??" + this.workOverTimeFormBean.getPersonCodeList().get(i));
			//System.out.println("~~~~~~~~~~~~~~~~~~~???" + this.workOverTimeFormBean.getOvertimebegindayList().get(i)
			//		+ this.workOverTimeFormBean.getOvertimeenddayList().get(i));
			//System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			/*System.out.println(this.workOverTimeFormBean.getOvertimeenddayList().get(i)
					+ this.workOverTimeFormBean.getOvertimesumhourList().get(i)
					+ this.workOverTimeFormBean.getReasonList().get(i)
					+ this.workOverTimeFormBean.getRemarkList().get(i)
					+ this.workOverTimeFormBean.getOvertimetypeList().get(i));*/
			String code = this.workOverTimeFormBean.getOvertimetypeList().get(i);
			Connection conn = null;
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());
			// 工号查人
			// findPersonInfo(this.workOverTimeFormBean.getPsncode(), conn);
			// this.workOverTimeFormBean.getPersonCodeList().get(i)
			findPersonInfo((String)this.workOverTimeFormBean.getPersonCodeList().get(i), conn);
			// 组织查加班类型
			findovertimetype((String)this.workOverTimeFormBean.getPk_corpID(), conn, code);
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			// 定义请求报文对象
			List<GroupRecord> grs1 = new ArrayList<GroupRecord>();
			IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方) 系统号
			String ServiceID = "06003000000006";// 响应方(发布方) 服务号
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSerialNO(format.substring(0, 8) + SourceSysID + String.format("%06d", ++RequestNoTimerTask.requestNo));
			//reqMo.setSerialNO(format.substring(0, 8) + SourceSysID + "000001");// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format);
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setControlValue("", null);
			this.workOverTimeFormBean.getApplier();
			GroupRecord gr1 = new GroupRecord();
			gr1.setName("overTimePushHVO");
			gr1.setFieldValue("status", "1");
			gr1.setFieldValue("dirty", "1");
			gr1.setFieldValue("primaryKey", "2");
			gr1.setFieldValue("bILLSOURCE", " ");
			// 姓名 ID
			// this.workOverTimeFormBean.getApplier();
			gr1.setFieldValue("aPPLIER", this.workOverTimeFormBean.getPk_psndoc());
			gr1.setFieldValue("iSAPPBILL", "N");
			gr1.setFieldValue("pK_OVERTIME_H", "12123");
			gr1.setFieldValue("fUN_CODE", "6017100413");
			gr1.setFieldValue("aPPROVEIDEA", " ");
			gr1.setFieldValue("dR", "0");
			// 申请日期
			// this.workOverTimeFormBean.getApplydate();
			//System.out.println(this.workOverTimeFormBean.getApplydate().substring(0, 10));
			gr1.setFieldValue("aPPLYDATE", ((String)this.workOverTimeFormBean.getApplydate()).substring(0, 10));
			// 部门/车间 ID
			// this.workOverTimeFormBean.getApplydept();
			gr1.setFieldValue("aPPLYDEPT", this.workOverTimeFormBean.getPk_deptdoc());
			gr1.setFieldValue("aPPROVEDATE", " ");
			gr1.setFieldValue("aPPROVESTATUS", "200");
			// 唯一 ID
			// this.workOverTimeFormBean.getOvertimeid();
			//System.out.println(this.workOverTimeFormBean.getOvertimeid());
			String overtimeid=this.workOverTimeFormBean.getOvertimeid()+"0"+i;
			//System.out.println("!!!"+overtimeid);
			gr1.setFieldValue("oVERTIMEID",overtimeid);
			gr1.setFieldValue("iSHRSSBILL", "N");
			gr1.setFieldValue("tS", " ");
			gr1.setFieldValue("bILLNAME",overtimeid);
			gr1.setFieldValue("sUBMITTER", " ");
			// 公司名称 ID
			// this.workOverTimeFormBean.getPk_corp();
			gr1.setFieldValue("pK_CORP", this.workOverTimeFormBean.getPk_corpID());
			gr1.setFieldValue("cENSOR", " ");
			gr1.setFieldValue("pK_BUSITYPE", " ");
			gr1.setFieldValue("sUBMIT_DATE", " ");
			gr1.setFieldValue("oVERTIMETYPE", this.workOverTimeFormBean.getPk_timeitem());
			grs1.add(gr1);
			GroupRecord gr2 = new GroupRecord();
			gr2.setName("overTimePushBVO");
			gr2.setFieldValue("status", "1");
			gr2.setFieldValue("dirty", "1");
			gr2.setFieldValue("primaryKey", "1");
			// 工号ID
			//System.out.println("~~~~~~~~~~~~~~~~~~~~" + this.workOverTimeFormBean.getPsncode());
			//System.out.println("~~~~~~~~~~~~~~~~~~~~" + this.workOverTimeFormBean.getPk_psndoc());
			gr2.setFieldValue("pSNCODE", this.workOverTimeFormBean.getPk_psndoc());
			// 备注
			// this.workOverTimeFormBean.getRemark();
			//System.out.println("Remark:" + this.workOverTimeFormBean.getRemarkList().get(i));
			gr2.setFieldValue("rEMARK",(String)this.workOverTimeFormBean.getRemarkList().get(i));
			// 加班开始日期
			//System.out.println("11111111111111");
			// this.workOverTimeFormBean.getOvertimebeginday();
			//System.out.println("date:" + this.workOverTimeFormBean.getOvertimebegindayList().get(i).substring(0, 10));
			// System.out.println("~~~~~~~~~~~~~~~~~~~~" +
			// this.workOverTimeFormBean.getOvertimebeginday().substring(0,
			// 10));
		//System.out.println("````````````````"+(String)this.workOverTimeFormBean.getOvertimebegindayList().get(i));
			gr2.setFieldValue("oVERTIMEBEGINDAY",
					((String)this.workOverTimeFormBean.getOvertimebegindayList().get(i)).substring(0, 10));
			gr2.setFieldValue("oVERTIMEPSNORDER", "1");
			gr2.setFieldValue("iSTOREST", "N");
			// 加班原因
			// this.workOverTimeFormBean.getReason();
	//System.out.println("reason:" +(String) this.workOverTimeFormBean.getReasonList().get(i));
			gr2.setFieldValue("rEASON", (String)this.workOverTimeFormBean.getReasonList().get(i));
			gr2.setFieldValue("kQENDDATE", " ");
			gr2.setFieldValue("dEDUCT", "0");
			gr2.setFieldValue("iSNEEDCHECK", "N");
			gr2.setFieldValue("pK_OVERTIME_B", "123132123");
			gr2.setFieldValue("pK_OVERTIME_H", "23132123");
			gr2.setFieldValue("dR", "0");
			gr2.setFieldValue("pLANBEGINTIME", "08:00:00");
			gr2.setFieldValue("iFAPPROVE", "N");
			gr2.setFieldValue("tS", "1");
			gr2.setFieldValue("kQENDTIME", " ");
			gr2.setFieldValue("iSCHECK", "N");
			// 加班结束日期 this.workOverTimeFormBean.getOvertimebegindayList().get(i)
			// System.out.println(this.workOverTimeFormBean.getOvertimeendday().substring(0,
			// 10));
			//System.out.println(this.workOverTimeFormBean.getOvertimeenddayList().get(i).substring(0, 10));
			gr2.setFieldValue("oVERTIMEENDDAY",(String)this.workOverTimeFormBean.getOvertimeenddayList().get(i).substring(0, 10));
			gr2.setFieldValue("pK_TBM_PSNDOC", this.workOverTimeFormBean.getPK_TBM_PSNDOC());
			gr2.setFieldValue("sUMHOUR",(String)this.workOverTimeFormBean.getOvertimesumhourList().get(i));
			// 加班开始时间
			// this.workOverTimeFormBean.getOvertimebeginday().substring(0, 10)
			//System.out.println(this.workOverTimeFormBean.getOvertimebegindayList().get(i).substring(11, 19));
			// gr2.setFieldValue("oVERTIMEBEGINTIME",
			// this.workOverTimeFormBean.getOvertimebeginday().substring(11,
			// 19));
			gr2.setFieldValue("oVERTIMEBEGINTIME",
					((String)this.workOverTimeFormBean.getOvertimebegindayList().get(i)).substring(11, 19));
			gr2.setFieldValue("pLANBEGINDAY", "2013-05-25");
			gr2.setFieldValue("tORESTHOUR", "8.0000");
			gr2.setFieldValue("pLANENDDAY", "2013-05-25");
			gr2.setFieldValue("kQSTARTTIME", " ");
			gr2.setFieldValue("oVERTIMEALREADY", "0.00");
			// 加班结束时间
			//System.out.println((String)this.workOverTimeFormBean.getOvertimeenddayList().get(i).substring(11, 19));
			gr2.setFieldValue("oVERTIMEENDTIME",
					((String)this.workOverTimeFormBean.getOvertimeenddayList().get(i)).substring(11, 19));
			gr2.setFieldValue("pLANENDTIME", "08:00:00");
			gr2.setFieldValue("kQSTARTDATE", " ");
			// 加班时间合计
			// this.workOverTimeFormBean.getOvertimesumhour();
			gr2.setFieldValue("oVERTIMESUMHOUR",(String)this.workOverTimeFormBean.getOvertimesumhourList().get(i));
			// 是否国家法定节假日
			// this.workOverTimeFormBean.getOvertimetype();
			//System.out.println("oVERTIMETYPE:"+this.workOverTimeFormBean.getPk_timeitem());
			gr2.setFieldValue("oVERTIMETYPE", this.workOverTimeFormBean.getPk_timeitem());
			grs1.add(gr2);
			reqMo.setReqGroupRecord(grs1);
			//System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~" + reqMo);

			IMsgObject respMo = null;
			respMo = Requester.execute(reqMo);
			//System.out.println("respMo:" + respMo);
			if (respMo.getResValue("return").equals("ok")) {
			//	System.out.println("Overtime OK");
				log.info("Overtime OK");
				success = true;
			} else {
			//System.out.println("Overtime FAIL");
				log.info("Overtime FAIL");
			}
		}

		return success;

	}

	public WorkOverTimeFormBean getWorkOverTimeFormBean() {
		return workOverTimeFormBean;
	}

	public void setWorkOverTimeFormBean(WorkOverTimeFormBean workOverTimeFormBean) {
		this.workOverTimeFormBean = workOverTimeFormBean;
	}

	private void findPersonInfo(String code, Connection conn) throws Exception {
		conn.setAutoCommit(false);
		PreparedStatement st = null;
		ResultSet rs = null;
		StringBuilder sb = new StringBuilder();
		sb.append(FIND_PERSONINFO_STR);
		//System.out.println("~~~~~~~~~~~~~~~~~~~~~~~8" + sb.toString());
		st = conn.prepareStatement(sb.toString());
		sb = null;
		st.setString(1, code);
		rs = st.executeQuery();

		while (rs.next()) {
			workOverTimeFormBean.setPk_psndoc(rs.getString(1));
			workOverTimeFormBean.setPK_TBM_PSNDOC(rs.getString(2));
			workOverTimeFormBean.setPk_deptdoc(rs.getString(3));
			workOverTimeFormBean.setPk_om_job(rs.getString(4));
			workOverTimeFormBean.setPk_corpID(rs.getString(5));
		}
		conn.commit();
		DBUtil.close(rs);
		DBUtil.close(st);
	}

	private void findovertimetype(String pk_corp, Connection conn, String code) throws Exception {
		conn.setAutoCommit(false);
		PreparedStatement st = null;
		ResultSet rs = null;
	StringBuilder sb = new StringBuilder();
	sb.append(FIND_OVERTIMETYPE_STR);
	try {	st = conn.prepareStatement(sb.toString());
	sb = null;
	st.setString(1, pk_corp);
	rs = st.executeQuery();
	String[] str = new String[3];
	int i = 0;
	// System.out.println(this.workOverTimeFormBean.getOvertimetype() 
	while (rs.next()) {
		str[i] = rs.getString(1);
		i++;
	}

	if (code.equals("0")) {
		workOverTimeFormBean.setPk_timeitem(str[0]);
	//	System.out.println("&&&&&&" + str[0]);
	} else if (code.equals("1")) {
		workOverTimeFormBean.setPk_timeitem(str[1]);
	//	System.out.println("&&&&&&" + str[1]);
	} else {
		workOverTimeFormBean.setPk_timeitem(str[2]);
		//System.out.println("&&&&&&" + str[2]);
	}
	
} catch (Exception e) {
	e.printStackTrace();
}finally{
	conn.commit();
	DBUtil.close(rs);
	DBUtil.close(st);
}			
	}
}
